package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.pojo.OrderSettingList;
import com.itheima.service.OrderSettingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = OrderSettingListService.class)
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<OrderSettingList> page = orderDao.selectByCondition(queryString);
        List<OrderSettingList> rows = page.getResult();
        long total = page.getTotal();
        return new PageResult(total,rows);
    }

    @Override
    public void addOrderSettingList(OrderSettingList orderSettingList) {
        String phoneNumber = orderSettingList.getPhoneNumber();
        Member member = memberDao.findByPhoneNumber(phoneNumber);
        if (member!=null){
            Integer id = member.getId();
            //System.out.println(id);
            orderSettingList.setId(id);
            orderDao.addOrderSettingList(orderSettingList);
        }else {
            memberDao.addOrderSettingList(orderSettingList);
            Integer id = orderSettingList.getId();
            System.out.println(id);
            orderDao.addOrderSettingList(orderSettingList);
        }
    }

    /**
     * 通过id删除order数据
     * @param id
     */
    @Override
    public void delete(int id) {
        orderDao.delete(id);
    }

     /**
     * 批量取消预约
     * @param orderSettingLists
     */
    @Override
    public void deletes(List<OrderSettingList> orderSettingLists) {
        if (orderSettingLists!=null && orderSettingLists.size()>0) {
            for (OrderSettingList orderSettingList : orderSettingLists) {
                Integer id = orderSettingList.getId();
                System.out.println(id);
                orderDao.delete(id);
            }
        }
    }
}
